<?php


class reportallocationmodel extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    public function getdata()
    {
        $region = $this->input->post('region_name');
        $location = $this->input->post('location_name');
        if (($region == FALSE) && ($location == FALSE)) {
            $query = $this->db->get('allocations');
        } else
            if (($region == TRUE) && ($location == TRUE)) {
                $query = $this->db->get_where('allocations', array('region' => $region, 'location' => $location));
            } else if ($location == FALSE) {
                $query = $this->db->get_where('allocations', array('region' => $region));
            } else {
                $query = $this->db->get_where('allocations', array('location' => $location));
            }
        return $query->result_array();
    }

    public function getinventorystatistics()
    {
        return $this->db->count_all('allocations');
    }

    public function getinventoryofregion($region)
    {
        $this->db->where('region', $region);
        $this->db->from('allocations');
        return $this->db->count_all_results();
    }

    public function getinventoryoflocation($location)
    {
        $this->db->where('location', $location);
        $this->db->from('allocations');
        return $this->db->count_all_results();
    }

    public function getreportofallocateddevices()
    {
        $this->load->helper('date');
        $this->db->distinct();
        $this->db->from('allocations');
        $this->db->join('devices', 'devices.serial_id = allocations.serial_id', 'inner');
        $this->db->where('status', 'allocated');
        $datestring = "%M/%D/%Y";
        $date = mdate($datestring);
        $this->db->where('replacement_date <', $date);
        $this->db->where('warranty_expiry <', $date);
        $query = $this->db->get();
        return $query->result_array();
    }
}